<?php
class class_CheckPlanilla
{
	private $_db; 
	function __construct()
	{
		$this->config();
	}
	function method_getRegistros($params, $error)
    {
        if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }

//         $this->_db->setQuery("SELECT tabla FROM obras_sociales WHERE cod_obra_social = '" . $params[0]->os . "'");
//     	$this->_db->alter();
//     	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
//         $tabla = $this->_db->loadResult();
//         $tabla = $tabla[0]->tabla;
    	
        $q = mysql_query("
        SELECT 
        turnos_os.cod_turno, 
        DATE_FORMAT(turnos.fecha_asignada, '%d/%m/%Y') as fecha, 
        CONCAT(pacientes.apellido, ', ', pacientes.nombres) as paciente,
        prestaciones.descripcion as prestacion, 
        turnos_os.importe, 
        cobrado, 
        turnos_os.estado 
        FROM turnos_os 
        INNER JOIN nomenclador ON turnos_os.cod_nomenclador = nomenclador.cod_nomenclador 
        INNER JOIN prestaciones ON nomenclador.cod_prestacion = prestaciones.cod_prestacion
        INNER JOIN turnos ON turnos_os.cod_turno = turnos.cod_turno
        INNER JOIN pacientes ON turnos.dni_paciente = pacientes.dni_paciente
        WHERE cod_planilla_cobro = '" . $params[0]->cod_planilla_cobro . "'
        ORDER BY fecha_asignada, hora_asignada
        ");
//         $this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	$res = Array();
    	while ($r = mysql_fetch_object($q)) {
    		$res []= $r;
    	}
    	return $res;
    }
    function method_getPlanillasOS($params, $error)
    {
    	if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
        
//        $this->_db->setQuery("SELECT cod_planilla_cobro, descripcion, DATE_FORMAT(fecha, '%d/%m/%Y') as fecha, hora FROM pla_" . $params[0]->os . " WHERE estado = 'N'");
//        $this->_db->alter();
//        if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	$this->_db->setQuery("
    	SELECT 
    	cod_planilla_cobro, 
    	CONCAT('Nro: ', nro_factura, ' - ', tipo_factura) as factura,
    	DATE_FORMAT(fecha, '%d/%m/%Y') as fecha,
    	hora 
    	FROM planillas 
    	WHERE estado = 'N'
    	AND cod_obra_social = '" . $params[0]->cod_os . "'
    	AND cod_plan = '" . $params[0]->cod_plan . "'
    	");
        $this->_db->alter();
        if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
            	
        $regs = $this->_db->loadResult();
		$result = array();
        FOREACH ($regs as $reg)
		{
			$row = array();
			array_push($row, $reg->cod_planilla_cobro);
			array_push($row, $reg->factura);
			array_push($row, $reg->fecha);
			array_push($row, $reg->hora);
			array_push($result, $row);
		}
    	return $result;
    }
    function method_marcarCobrado($params, $error)
    {
    	if (count($params) != 1) { $error->SetError(JsonRpcError_ParameterMismatch, "Expected 1 parameter; got " . count($params)); return $error; }
    	
    	
//     	$this->_db->setQuery("SELECT tabla FROM obras_sociales WHERE cod_obra_social = '" . $params[0]->tabla ."' LIMIT 1");
// 		$this->_db->alter();
//         if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
// 		$tabla = $this->_db->loadResult();
        
		mysql_query("START TRANSACTION");
        if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
		
		$this->_db->setQuery("
		UPDATE planillas SET 
		fecha = '" . date("Y-m-d") . "', 
		hora = '" . date("H:i:s") . "', 
		estado = 'C',
		forma_cobro = '" . $params[0]->forma_cobro . "',
		observaciones = '" . $params[0]->observaciones . "'
		WHERE cod_planilla_cobro = '" . $params[0]->cod_planilla_cobro . "' 
		AND estado = 'N'");
        $this->_db->alter();
        if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); mysql_query("ROLLBACK"); return $error; }
        
        $SQL = "";
        FOREACH ($params[0]->registros as $reg)
        {
        	//$SQL .="UPDATE os_" . $params[0]->tabla . " SET cobrado = '" . $reg->cobrado . "', estado = '" . $reg->estado . "' WHERE cod_planilla_cobro = '" . $params[0]->cod_planilla_cobro . "' AND estado = 'P' AND cod_turno = '" . $reg->cod_turno . "'";
        	IF (($reg->estado == 'R') || ($reg->estado == 'I'))
        	{
        		$reg->cobrado = 'importe';
        	}
        	else
        	{
        		$reg->cobrado = "'" . $reg->cobrado . "'";
        	}
	    	$this->_db->setQuery("
	    	UPDATE turnos_os SET
	    	cobrado = " . $reg->cobrado . ", 
	    	estado = '" . $reg->estado . "' 
	    	WHERE cod_planilla_cobro = '" . $params[0]->cod_planilla_cobro . "' 
	    	AND estado = 'P' 
	    	AND cod_turno = '" . $reg->cod_turno . "'
	    	LIMIT 1");
	        $this->_db->alter();
	        if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); mysql_query("ROLLBACK"); return $error; }
        }
        mysql_query("COMMIT"); 
    }
    
    function method_getObrasSociales($params, $error)
    {
    	$this->_db->setQuery("SELECT cod_obra_social as value, descripcion as label FROM obras_sociales ORDER BY descripcion");
    	$this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	return $this->_db->loadResult();
    }
    
    function method_getPlanes($params, $error)
    {
    	$this->_db->setQuery("
		(SELECT cod_plan as value, descripcion as label 
		FROM planes 
		WHERE cod_obra_social = '" . $params[0]->cod_os . "' 
		ORDER BY descripcion)");
    	$this->_db->alter();
    	if (mysql_error()) { $error->SetError(JsonRpcError_Unknown, (__FILE__ . " - " . (__LINE__ - 2) . ": " . mysql_error())); return $error; }
    	return $this->_db->loadResult();
    }
    
	private function config(){
		require_once("conexion.php");
		require_once("DataBase.php");
		$this->_db = new DataBase($SERVIDOR,$USUARIO,$PASSWORD,$BASE);
		mysql_query("SET NAMES utf8");
	}
}
?>